﻿using System.Windows;
using System.Windows.Interactivity;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace OpenLightGroup.Common.Behaviors
{
    /// <summary>
    /// Serves as a base class to implement behaviors based on the rules the current user is in.
    /// </summary>
    /// <typeparam name="T">The type of  dependency object the behavior can be associated with.</typeparam>
    public abstract class BaseRoleBasedBehavior<T>
        : Behavior<T> where T : DependencyObject
    {

        /// <summary>
        /// The list of allowed roles to be used in this behavior.
        /// </summary>
        [Category("Security Settings")]
        [Description("Associated control will only be visible if the current user is a member of one or more of these roles.")]
        public  Collection<string> AllowedRoles { get; private set; }
 

        /// <summary>
        /// Default constructor for this class. Initializes the AllowedRoles collection and wires up the event handlers for 
        /// when a user is logged in or out.
        /// </summary>
        protected BaseRoleBasedBehavior()
        {
            this.AllowedRoles = new Collection<string>();

            if (DesignTimeHelper.IsRuntime)
            {

                WebContextBase.Current.Authentication.LoggedIn += 
                    new System.EventHandler<AuthenticationEventArgs>(UpdateAssociatedObject);

                WebContextBase.Current.Authentication.LoggedOut += 
                    new System.EventHandler<AuthenticationEventArgs>(UpdateAssociatedObject);

            }
        }

        /// <summary>
        /// Method that must be overridden in a subclass that allows the behavior to respond to the LoggedIn and LoggedOut events.
        /// </summary>
        /// <param name="sender">The instance of WebContextBase that raised the event.</param>
        /// <param name="e">The authentication event argument that is returned by the WebContextBase event(s).</param>
        protected abstract void UpdateAssociatedObject(object sender, AuthenticationEventArgs e);

        /// <summary>
        /// Overrides the base Behavior class method. This method will also call the UpdateAssociatedObject method.
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();

            this.UpdateAssociatedObject(WebContextBase.Current, null);
        }

        /// <summary>
        /// Overrides the base Behavior class method. This method handles removing the event handlers to WebContextBase.
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();

            WebContextBase.Current.Authentication.LoggedIn -= UpdateAssociatedObject;
            WebContextBase.Current.Authentication.LoggedOut -= UpdateAssociatedObject;
        }

        /// <summary>
        /// Check to see if the current use is a member of the specified role.
        /// </summary>
        /// <param name="roleName">Role to check membership of.</param>
        /// <returns></returns>
        protected bool IsInRole(string roleName)
        {

            if (DesignTimeHelper.IsRuntime)
            {
                return WebContextBase.Current.Authentication.User.IsInRole(roleName);
            }
            else
            {
                return true;

            }
        }

        
    }
}

